package com.xgkj.diyiketang.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * 项目名称：Sosoliuda
 * 类描述：时间操作相关的工具类
 * 创建人：资拥
 * 创建时间：16/9/22 下午2:07
 * 修改人：资拥
 * 修改时间：16/9/22 下午2:07
 * 修改备注：
 */
public class DateUtil {

    private static SimpleDateFormat sdf = null;


    /**
     * 获取系统时间
     * 格式为：yyyy/MM/dd
     */
    public static String getCurrentDate() {
        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }


    /**
     * 获取系统日期
     * 格式为：yyyy/MM/dd
     */
    public static String getCurrentDateOnly() {
        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 按照自定义格式获取当前时间
     *
     * @param timeFormat
     * @return
     */
    public static String getCustomCurrentDate(String timeFormat) {
        Date date = new Date();
        sdf = new SimpleDateFormat(timeFormat);
        return sdf.format(date);
    }


    /**
     * 将时间格式字符串转换为时间 yyyy-MM-dd hh:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 将时间戳转换为字符串
     */
    public static String getDateToString(long time) {
        Date date = new Date(time);
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 将时间戳转换为字符串
     */
    public static String getDateToStringOnly(long time) {
        Date date = new Date(time);
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 将字符串转换为时间戳
     */
    public static long getStringToDate(String time) {
        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static long getStringToDate2(String time) {
        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 将字符串转换为时间戳
     */
    public static long getStringToMilles(String time) {

        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime() / 1000;
    }

    public static int[] getCalendarDate() {
        int[] date = new int[6];
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        date[0] = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH);
        date[1] = m + 1;
        date[2] = c.get(Calendar.DATE);
        date[3] = c.get(Calendar.HOUR_OF_DAY);
        date[4] = c.get(Calendar.MINUTE);
        date[5] = c.get(Calendar.SECOND);
        return date;
    }


    public static String getCurrentMonthFirstDay() {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = sdf.format(gcLast.getTime());
        return day_first;
    }


    public static int getCurrentWeek() {
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sdf.parse(getCurrentDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        return weekOfMonth;
    }


    public static String getHourDateByIndex(int hour) {
        StringBuilder strBuilder = new StringBuilder();
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        strBuilder.append(c.get(Calendar.YEAR) + "-");
        int m = c.get(Calendar.MONTH);
        m = m + 1;
        strBuilder.append(m + "-");
        strBuilder.append(c.get(Calendar.DATE) + " ");
        strBuilder.append(hour).append(":00:").append("00");
        return strBuilder.toString();
    }


    //获得一年前的日期
    public static String getLastYear() {
        int[] date = new int[3];
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        date[0] = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH);
        date[1] = m + 1;
        date[2] = c.get(Calendar.DATE);
        return date[0] - 1 + "-" + date[1] + "-" + date[2];
    }


    /**
     * 获得当前年月日星期
     * 返回格式 2016.8.22 - 周一
     *
     * @return
     */
    public static String getTimeStr() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mYear + "." + mMonth + "." + mDay + " - " + "  " + "周" + mWay;
    }

    public static String millesToTime(long milles) {
        long hour = milles / (1000 * 60 * 60);
        long min = (milles / (1000 * 60)) % 60;
        long second = (milles / 1000) % 60;
        return hour + "小时" + min + "分钟" + second + "秒";
    }

    /**
     * 取本周7天的第一天（周一的日期）
     *
     * @return
     */
    public static long getNowWeekBegin() {
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            mondayPlus = 0;
        } else if (dayOfWeek == 0) {
            mondayPlus = -6;
        } else {
            mondayPlus = 1 - dayOfWeek;
        }
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        getTodayZero(monday);
        return getTodayZero(monday);
    }


    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            return 0;
        } else if (dayOfWeek == 0) {
            return -6;
        } else {
            return 1 - dayOfWeek;
        }
    }


    /**
     * 取本月的第一天日期的毫秒数
     *
     * @return
     */
    public static long getNowMonthBegin() {
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
//设置为第一天
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        Date date = gcLast.getTime();
        return getTodayZero(date);
    }

    /**
     * 获得今天零点的毫秒数
     *
     * @return
     */
    public static long getTodayZero() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UTC+8"));
        cal.setTime(date);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        System.out.println("today zero : " + cal.getTimeInMillis());
        return getZeroTime(cal.getTimeInMillis());
    }

    /**
     * 获得某天零点的毫秒数
     *
     * @return
     */
    public static long getTodayZero(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UTC+8"));
        cal.setTime(date);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.SECOND, 1);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        System.out.println("today zero : " + cal.getTimeInMillis());
        return getZeroTime(cal.getTimeInMillis());
    }

    /**
     * 获得前num个月的一号的时间毫秒数
     *
     * @param num 前几个月
     * @return
     */
    public static long getMillesOfMonthsAgo(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -num);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date theDate = calendar.getTime();
        return getTodayZero(theDate);
    }

    //获取yyyy-MM-dd是星期几
    public static String getWeekdayOfDateTime(String datetime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(df.parse(datetime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "星期" + mWay;
    }

    private static long getZeroTime(long time) {
        return time - 8 * 60 * 60 * 1000;
    }

    /**
     * 获取系统时间戳
     */
    public static long getCurrentTime() {
        return new Date().getTime();
    }

    /**
     * @param
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author fy.zhang
     */
    public static String formatDuring(long mss) {
        if (mss <= 0) {
            return "已超时";
        } else {
            long days = mss / (1000 * 60 * 60 * 24);
            long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
            long seconds = (mss % (1000 * 60)) / 1000;
            if (days <= 0) {
                return hours + "小时" + minutes + "分钟"
                        + seconds + "秒";
            } else if (days <= 0 && hours <= 0) {
                return minutes + "分钟"
                        + seconds + "秒";
            } else if (days <= 0 && hours <= 0 && minutes <= 0) {
                return seconds + "秒";
            } else if (days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0) {
                return "已超时";
            } else {
                return days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
            }
        }
    }
}
